//
//  LRXRealNameController.m
//  路人侠
//
//  Created by zhangxulong on 5/29/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXRealNameController.h"
#import "simpleNetTask.h"
@interface LRXRealNameController ()<UITextFieldDelegate>
@property (nonatomic,strong) NSMutableArray * textFields;
@property (nonatomic,weak) UIView * contentView;
@end

@implementation LRXRealNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.textFields = [NSMutableArray array];
    self.navigationItem.title = @"实名认证";
    self.view.backgroundColor = [UIColor colorWithRed:244 green:244 blue:244];
    UIImageView * headImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"realName"]];
    headImage.frame = CGRectMake(0, 0, self.view.frame.size.width, 400 * hScale);
    [self.view addSubview:headImage];
    [self layoutContentView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willHide) name:UIKeyboardWillHideNotification object:nil];

}
- (void)layoutContentView
{
    UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 470 * hScale, self.view.frame.size.width, 288 * hScale)];
    contentView.backgroundColor = [UIColor whiteColor];
    self.contentView = contentView;
    
    
    UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(33 * wScale, 41 * hScale, 0, 0)];
    nameLabel.font = [UIFont systemFontOfSize:28 *hScale];
    nameLabel.text = @"姓名:";
    [nameLabel sizeToFit];
    
    UITextField * nameText = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(nameLabel.frame) + 27 * wScale, CGRectGetMinX(nameLabel.frame), 500 * wScale, 30 * hScale)];
    nameText.placeholder = @"请填写真实的姓名";
    nameText.font = [UIFont systemFontOfSize:27 * hScale];
    nameText.tag = 0;
    CGFloat minX = CGRectGetMaxX(nameLabel.frame) + 25 * wScale;
    nameText.delegate = self;
    nameText.returnKeyType = UIReturnKeyNext;
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(minX, CGRectGetMaxY(nameText.frame) + 10 * hScale, self.view.frame.size.width - minX, 2 * hScale)];
    lineView.backgroundColor = GraylineColor;
                                                                           
    [contentView addSubview:nameLabel];
    [contentView addSubview:nameText];
    [contentView addSubview:lineView];
    
    
    
    CGFloat stuNOY = CGRectGetMaxY(lineView.frame) + 47 * hScale;
    UILabel * stuNOLabel = [[UILabel alloc]initWithFrame:CGRectMake(33 * wScale, stuNOY, 0, 0)];
    stuNOLabel.font = [UIFont systemFontOfSize:28 *hScale];
    stuNOLabel.text = @"学号:";
    [stuNOLabel sizeToFit];
    
    UITextField * stuNOText = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(stuNOLabel.frame) + 27 * wScale, CGRectGetMinY(stuNOLabel.frame), 500 * wScale, 30 * hScale)];
    stuNOText.placeholder = @"请输入教务账号";
    stuNOText.font = [UIFont systemFontOfSize:27 * hScale];
    stuNOText.tag = 1;
    stuNOText.delegate = self;
    stuNOText.returnKeyType = UIReturnKeyNext;
    UIView * lineView2 = [[UIView alloc]initWithFrame:CGRectMake(minX, CGRectGetMaxY(stuNOText.frame) + 10 * hScale, self.view.frame.size.width - minX, 2 * hScale)];
    lineView2.backgroundColor = GraylineColor;
    [contentView addSubview:stuNOLabel];
    [contentView addSubview:stuNOText];
    [contentView addSubview:lineView2];
    
    
    
    CGFloat passY = CGRectGetMaxY(lineView2.frame) + 47 * hScale;
    UILabel * passLabel = [[UILabel alloc]initWithFrame:CGRectMake(33 * wScale, passY, 0, 0)];
    passLabel.font = [UIFont systemFontOfSize:28 *hScale];
    passLabel.text = @"密码:";
    [passLabel sizeToFit];
    
    UITextField * passText = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(passLabel.frame) + 27 * wScale, CGRectGetMinY(passLabel.frame), 500 * wScale, 30 * hScale)];
    passText.placeholder = @"请输入教务网密码";
    passText.font = [UIFont systemFontOfSize:27 * hScale];
    passText.tag = 2;
    passText.secureTextEntry = YES;
    passText.delegate = self;
    UIView * lineView3 = [[UIView alloc]initWithFrame:CGRectMake(minX, CGRectGetMaxY(passText.frame) + 10 * hScale, self.view.frame.size.width - minX, 2 * hScale)];
    lineView3.backgroundColor = GraylineColor;
    
    
    [contentView addSubview:passLabel];
    [contentView addSubview:passText];
    [contentView addSubview:lineView3];
    
    [self.view addSubview:contentView];
    
    UIImageView * image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"waring"]];
    image.frame = CGRectMake(69 * wScale, CGRectGetMaxY(contentView.frame) + 16 * hScale, 28 * hScale,28 * hScale);
    
    UILabel * remindlabel = [[UILabel alloc]initWithFrame:CGRectMake(122 * wScale, CGRectGetMaxY(contentView.frame) + 20 * hScale, 0, 0)];
    remindlabel.text = @"我们将确保您的账户安全，请放心使用";
    remindlabel.font = [UIFont systemFontOfSize:20 *hScale];
    remindlabel.textColor = [UIColor colorWithRed:255 green:156 blue:0];
    [remindlabel sizeToFit];
    
    
    [self.view addSubview:image];
    [self.view addSubview:remindlabel];
    
    
    UIButton * ppp = [[UIButton alloc]initWithFrame:CGRectMake(MainScreenW * 0.5  - 230 * wScale, 840 * hScale, 460 * wScale, 80 * hScale)];
    [ppp setTitle:@"立即认证" forState:UIControlStateNormal];
    ppp.backgroundColor = mainColor;
    ppp.layer.cornerRadius = 10 *hScale;
    ppp.titleLabel.font = [UIFont systemFontOfSize:32 * hScale];
    [ppp setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [ppp addTarget:self action:@selector(pppClick:) forControlEvents:UIControlEventTouchUpInside];

    
    [self.view addSubview:ppp];
    
    [self.textFields addObject:nameText];
    [self.textFields addObject:stuNOText];
    [self.textFields addObject:passText];
}


#pragma mark - 键盘配合textField的view
-(void)willShow:(NSNotification *)notifi{
    
    UITextField *currentTf = self.textFields[[self indexOfFirstResponder]];
    CGFloat maxY = CGRectGetMaxY(currentTf.frame) + self.contentView.frame.origin.y;
    CGRect kbEndFrm = [notifi.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat kbY = kbEndFrm.origin.y;

    CGFloat delta = kbY - maxY - 50 * hScale;
    if(delta < 0){
        [UIView animateWithDuration:0.25 animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, delta);
        }];
    }
}
-(void)willHide{
    [UIView animateWithDuration:0.25 animations:^{
        self.view.transform = CGAffineTransformIdentity;
    }];
}

-(NSInteger)indexOfFirstResponder{
    
    for (UITextField * tf in self.textFields) {
        if (tf.isFirstResponder) {
            return tf.tag;
        }
    }
    //返回一1没有当前响应者
    return 0;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 0) {
        UITextField * nextField = [self.view viewWithTag:1];
        [nextField becomeFirstResponder];
    }
    else if(textField.tag == 1){
        UITextField * nextField = [self.view viewWithTag:2];
        [nextField becomeFirstResponder];
    }
    else if(textField.tag == 2)
    {
         [self.view endEditing:YES];
    }
    return YES;
    
}
#pragma mark - 认证的按钮
- (void)pppClick:(UIButton *)btn
{
//    simpleNetTaskCallBackBlock callBackblock = ^(NSDictionary * responceObject){
//    };
//    simpleNetTask * dataTask = [[simpleNetTask alloc]initWithLoadDataBlock:callBackblock];
    
    [MBProgressHUD showMessage:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUD];
        [self dismissViewControllerAnimated:YES completion:nil];
    });
    
    
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //键盘退出
    [self.view endEditing:YES];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
